package dvsdk.form;

import dvsdk.client.BinResult;
import dvsdk.db.Base;
import dvsdk.db.BinBase;
import dvsdk.db.BinParam;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Develop
 */
public class ExUtil {
    
    public static void initReTable(Base base, String name, String fields) {
        //Base base = new Base();
        base.reinit(name, fields);
        //base.disconnect();
    }
    
    public synchronized static List<BinBase> select(Base base, String base_name, String fields, String where) {
        return base.getAllElementsSync(base_name, fields, where);
    }
    
    public synchronized static void fillTabelData(Base base, JTable table, String name, String fields) {
        fillTabelData(base, table, name, fields, "");
    }
    /**
     * Запрос из базы всех данных и вставка их в таблицу
     * @param base
     * @param table
     * @param name
     * @param fields 
     */
    public synchronized static void fillTabelData(Base base, JTable table, String name, String fields, String where) {
        // TODO: ДОДЕЛАТЬ!!!
        List<BinBase> binBases = select(base, name, fields, where);
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setNumRows(0);
        //tableModel.addRow(new Object[]{"blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah","blah"});
        //TableModel tableModel = table.getModel();
        //tableModel.setColumnCount(2);
        //Object[] colNames = fields.split(",");
        //Object[][] rowData = {colNames,colNames};
        //table = new JTable(rowData, colNames);
        //table.
        int i=0;
        for (Iterator<BinBase> it = binBases.iterator(); it.hasNext();) {
            BinBase binBase = it.next();
            //tableModel.insertRow(table.getRowCount(),(GlobalConfig.STD_FILEDS+","+fields).split(","));
            Vector v = new Vector();
            v.add(binBase.getId());
            v.add(binBase.getIda());
            v.add(binBase.getIdr());
            v.add(binBase.getState());
            v.add(binBase.getDate_add());
            v.add(binBase.getDate_mod());
            List<BinParam> binParam = binBase.getParams();
            for (Iterator<BinParam> it1 = binParam.iterator(); it1.hasNext();) {
                BinParam binParam1 = it1.next();
                v.add(binParam1.getValue());
            }
            tableModel.insertRow(table.getRowCount(),v);
            
        }
    }
    
    public synchronized static void initTable(Base base, String name, String fields) {
        base.init(name, fields);
    }
    
    public synchronized static void insert(Base base, String base_name,BinResult result) {
        List<BinBase> bbs = new ArrayList();
        bbs.add(result);
        base.SyncAddElements(base_name, bbs);
    }
    
    public synchronized static void insert(Base base, String base_name, List<BinResult> result) {
        List<BinBase> bbs = new ArrayList();
        bbs.addAll(result);
        base.SyncAddElements(base_name, bbs);
    }
    
    public synchronized static void delete(Base base, String base_name, BinResult result) {
        List<BinBase> bbs = new ArrayList();
        bbs.add(result);
        base.SyncDelElements(base_name, bbs);
    } 
    
    public synchronized static void delete(Base base, String base_name, List<BinResult> result) {
        List<BinBase> bbs = new ArrayList();
        bbs.addAll(result);
        base.SyncDelElements(base_name, bbs);
    }
    
    public synchronized static void update(Base base, String base_name,BinResult result){
        List<BinBase> bbs = new ArrayList();
        bbs.add(result);
        base.SyncUpdElements(base_name, bbs);
    }
    
    public synchronized static void update(Base base, String base_name, List<BinResult> result){
        List<BinBase> bbs = new ArrayList();
        bbs.addAll(result);
        base.SyncUpdElements(base_name, bbs);
    }
    
    public synchronized static void count(Base base, String base_name, String status){
        base.SyncCountElements(base_name, status);
    }
}